package com.songtech.position.controller;

import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.base.util.SysUUID;
import com.songtech.login.model.SysPosUser;
import com.songtech.login.util.AuthUtil;
import com.songtech.position.model.SysPosTrail;
import com.songtech.position.service.PositionService;
import com.songtech.utils.SignUtil;
import com.songtech.utils.WeChatUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description: 定位信息控制层
 * @Author: rentie
 * @Date: 2018/4/11 9:34
 */
@Controller
@RequestMapping("/position")
public class PositionController {
    private Logger logger = LoggerFactory.getLogger(PositionController.class);
    @Autowired
    private PositionService positionService;

    /**
     * 查询实时定位信息
     * @return
     */
    @RequestMapping(value = "/realtime",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult realtime(HttpSession session){
        /**
         * 获取session中到user信息
         */
        SysPosUser user=(SysPosUser)session.getAttribute("loginUser");
        String imei=user.getImei();
        //String imei="865205032426073";
        SysPosTrail bean=null;
        try {
            bean= positionService.queryRealTime(imei);

        } catch (Exception e) {
            logger.error("实时定位异常信息:"+e.getMessage(),e);
            return new JsonResult(bean, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(bean, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    /**
     * 查询实时定位信息
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping(value = "/history", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult history(String startTime,String endTime,Integer locationType,HttpSession session){
        /**
         * 获取session中到user信息
         */
        SysPosUser user=(SysPosUser)session.getAttribute("loginUser");
        String imei=user.getImei();
        //String imei="865205032426073";
        List<SysPosTrail> list=null;
        try {
            list= positionService.queryHistory(startTime,endTime,locationType,imei);
        } catch (Exception e) {
            logger.error("历史轨迹异常信息:"+e.getMessage(),e);
            return new JsonResult(list, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(list, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    /**
     * 主动定位
     * @param session
     */
    @RequestMapping(value = "/activePosition", method = RequestMethod.POST)
    @ResponseBody
    public void activePosition(HttpSession session){
        /**
         * 获取session中到user信息
         */
        SysPosUser user=(SysPosUser)session.getAttribute("loginUser");
        if (user!=null){
            positionService.activePosition(user.getImei());
        }
    }

    /**
     * 生成微信JS-SDK权限验证的签名
     * @param url 当前网页的URL(encodeURIComponent编码之后的)，不包含#及其后面部分
     * @return
     */
    @RequestMapping(value = "/getJsApiSign", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult getJsApiSign(String url){

        //缓存access_token和jsapi_ticket的单例类
        WeChatUtil weChatUtil = WeChatUtil.getInstance();

        Map<String,String> map = weChatUtil.getMap();

        //随机字符串
        String noncestr= SysUUID.generator();

        //有效的jsapi_ticket
        String jsApiTicket=map.get("jsApiTicket");

        //时间戳
        String timestamp=String.valueOf(System.currentTimeMillis()/1000);

        //签名
        String signature = null;

        Map<String, String> resultMap = new HashMap<>();

        resultMap.put("appId", AuthUtil.appid);
        resultMap.put("timestamp",timestamp);
        resultMap.put("noncestr",noncestr);
        try {
            url= URLDecoder.decode(url,"UTF-8");
            //签名
            signature = SignUtil.getJsSdkSign(noncestr, jsApiTicket, timestamp, url);
            resultMap.put("signature",signature);
            logger.info("JS-SDK签名:"+signature);
        } catch (Exception e) {
            logger.error("生成微信JS-SDK权限验证的签名异常信息:"+e.getMessage(),e);
            return new JsonResult(resultMap, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(resultMap, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }


}
